package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.*;


@Transactional
@Service
public class PermissionServiceImpl implements IPermissionService {
    private PermissionMapper permissionMapper;

    @Autowired
    public void setPermissionMapper(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);

    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> getList() {
        List<Permission> permissions = permissionMapper.selectAll();
        return permissions;
    }

    @Autowired
    private RequestMappingHandlerMapping rmhm;

    @Override
    public PageInfo<Permission> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> list = permissionMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void reload() {
        //获取数据库所有的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //获得一个set对象,将数据库获得的权限加入到set中,去判断;


        Set<String> set = new HashSet<>(permissions);
        //获取所有的请求方法

//
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        //获得Map的values
        Collection<HandlerMethod> values = handlerMethods.values();
        for (HandlerMethod m : values) {
            //判断是否存在贴了该注解的方法
            RequiredPermission methodAnnotation = m.getMethodAnnotation(RequiredPermission.class);
            //判断是否有注解
            if (methodAnnotation == null) {
                continue;
            } else {
                String name = methodAnnotation.name();
                String expression = methodAnnotation.expression();
                //这个权限表达式不在数据库中的话
                if (!permissions.contains(expression)) {
                    //不存在
                    //封装权限对象,并保存到数据库
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    permissionMapper.insert(permission);
//                    数据库的集合增加了这个表达式
//                    permissions.add(expression)
                } else {
                    //如果这个权限表达式在数据中的话,移出相应的set
                    set.remove(expression);
                }
            }

        }

//        循环结束之后
        //遍历剩下的set
        for (String e : set) {
            //数据库删除这个权限表达式
            permissionMapper.deleteByExpression(e);
        }
    }

    @Override
    public List<Permission> getPermissions(Long rId) {
        List<Permission> permissions = permissionMapper.selectByRid(rId);
        return permissions;
    }
}
